﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;

namespace MongoCamp
{
    /// <summary>
    /// MongoDB Server configuration package
    /// </summary>
    public sealed class MongoServerPack
    {
        #region Static method
        static MongoServerPack()
        {
            var defaultConfig = MongoSetting.FromAppSetting("Default");
            DefaultDB = new MongoServerPack(defaultConfig.ConnectionString, defaultConfig.DBName);

            var logConfig = MongoSetting.FromAppSetting("Log");
            LogDB = new MongoServerPack(logConfig.ConnectionString, logConfig.DBName);
        }

        // ReSharper disable InconsistentNaming
        /// <summary>
        /// Default DB MongoServer
        /// </summary>
        public static MongoServerPack DefaultDB { get; private set; }

        /// <summary>
        /// Log DB MongoServer
        /// </summary>
        public static MongoServerPack LogDB { get; private set; }
        // ReSharper restore InconsistentNaming
        #endregion Static method

        /// <summary>
        /// Creates a new instance by ConnectionString and DatabaseName
        /// </summary>
        /// <param name="connectionString">MongoUrl or connectionString</param>
        /// <param name="dbName">MongoDB name</param>
        public MongoServerPack(string connectionString, string dbName = "")
        {
            DatabaseName = dbName;
            // ignore connectionString error
            if (string.IsNullOrEmpty(connectionString)) return;

            var url = new MongoUrl(connectionString);
            _mongoClient = new MongoClient(url);

            if (string.IsNullOrEmpty(dbName))
                DatabaseName = url.DatabaseName;
        }

        ///// <summary>
        ///// Creates a new instance by MongoDBUrl.
        ///// </summary>
        ///// <param name="mongoUrl">MongoUrl or connection string</param>
        //public MongoServerPack(string mongoUrl)
        //    : this(mongoUrl, string.Empty)
        //{ }

        private readonly MongoClient _mongoClient;

        /// <summary>
        /// Current MongoDB Name
        /// </summary>
        public string DatabaseName { get; private set; }

        /// <summary>
        /// Get current MongoServer
        /// </summary>
        /// <returns>MongoServer</returns>
        public MongoServer GetServer()
        {
            // ReSharper disable once NotResolvedInText
            if (_mongoClient == null)
                throw new ArgumentNullException("connectionString", "the MongoDB connection string is empty.");

            return _mongoClient.GetServer();
        }

        /// <summary>
        /// Get MongoDatabase by database name
        /// </summary>
        /// <param name="dbName">new Name or</param>
        /// <returns>MongoDatabase</returns>
        public MongoDatabase GetDatabase(string dbName)
        {
            var server = GetServer();
            return server.GetDatabase(dbName);
        }

        /// <summary>
        /// Get server default MongoDatabase
        /// </summary>
        /// <returns>MongoDatabase</returns>
        // ReSharper disable once InconsistentNaming
        public MongoDatabase GetDefaultDB()
        {
            return GetDatabase(DatabaseName);
        }

    }
}
